package com.hz.eduService.controller;

import com.hz.apiService.model.request.ApiResponse;
import com.hz.eduService.domain.Course;
import com.hz.eduService.domain.UserCourse;
import com.hz.eduService.request.SelectCourseRequest;
import com.hz.eduService.service.CourseService;
import com.hz.eduService.service.UserCourseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user-edu")
public class UserEduController {

    @Resource
    UserCourseService userCourseService;
    @Resource
    CourseService courseService;

    @PostMapping("/student/select-course")
    public String selectCourse(@RequestParam("courseId") Integer courseId,@RequestParam("userId")Integer userId){
        int select = userCourseService.selectCourse(userId,courseId);
        return select>0 ? ApiResponse.ok(select) : ApiResponse.intError();
    }

    @PostMapping("/list-user-classes")
    public String searchUserClasses() {
        List<Course> result = courseService.searchAllCourses();
        return result != null ? ApiResponse.ok(result) : ApiResponse.intError();
    }

    @PostMapping("/list-teacher-classes")
    public String searchTeacherClasses(@RequestParam("teacherId") Integer teacherId) {
        List<Course> result = courseService.searchTeacherCourses(teacherId);
        return result != null ? ApiResponse.ok(result) : ApiResponse.intError();
    }

    @PostMapping("/list-all-classes")
    public String searchAllCourses() {
        List<Course> result = courseService.searchAllCourses();
        if (result == null) return ApiResponse.intError();
        return ApiResponse.ok(result);
    }

    @GetMapping("/student/selected")
    public String selected(@RequestParam("studentId") Integer studentId){
        return ApiResponse.ok(userCourseService.selectCourses(studentId));
    }

    @GetMapping("/student/selected-info")
    public String selectedInfo(@RequestParam("courseId")Integer courseId,@RequestParam("studentId")Integer studentId){
        return ApiResponse.ok(userCourseService.selectedCourseInfo(studentId,courseId));
    }

    @PostMapping("/view-user-classes")
    public String DetailCourse(@RequestParam("courseCode") String courseCode) {
        Course result = courseService.searchCourseByCode(courseCode);
        if (result == null) return ApiResponse.intError();
        return ApiResponse.ok(result);
    }

    @PostMapping("/se")
    public String selectCourse(@RequestBody SelectCourseRequest selectCourseRequest, HttpServletRequest request) {
        //1.判断选课人数是否已满
        int maxCount = selectCourseRequest.getMaxStudentCount();
        int selectedCount = selectCourseRequest.getSelectedCount();
        if (selectedCount >= maxCount) return ApiResponse.intError();

        //2.满足选课要求选课
        UserCourse userCourse = new UserCourse();
        userCourse.setCourseId(selectCourseRequest.getCourseId());
        userCourse.setCourseName(selectCourseRequest.getCourseName());
        userCourse.setCourseCode(selectCourseRequest.getCourseCode());
        userCourse.setStudentUserId(selectCourseRequest.getStudentUserId());
        userCourse.setTeacherUserId(selectCourseRequest.getTeacherUserId());
        userCourse.setStudentRealName(selectCourseRequest.getStudentRealName());
        userCourse.setTeacherRealName(selectCourseRequest.getTeacherRealName());

        //3.存入数据库
        boolean result = userCourseService.addUserCourse(userCourse);
        return result ? ApiResponse.ok(result) : ApiResponse.sysError();
    }

    @PostMapping("/student/cancel-course")
    public String cancelSelectCourse(@RequestParam("courseId") Integer courseId, @RequestParam("userId") Integer UserId) {
        Integer result = userCourseService.cancelCourse(courseId, UserId);
        return result > 0 ? ApiResponse.ok(result) : ApiResponse.intError();
    }
}
